// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var dialog = require("dialog");
	var validate = require("validate");
	var css_collection_lict = require("./commodityPriceCollectionList.css");
	var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var imgSrc;
	var init = function(num) {
		$(".table-section").removeClass("none");
		initEvent();
		inquirePriceCollectionList();
		router.init();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		$("#main").on("click", "#collectionSearch", function() {
			pageIndex = 1;
			pageSize = 10;
			totalPageCount = 1;
			totalRecordCount = 0; //分页初始化
			inquirePriceCollectionList();
		});

		$("#pieceCollectionBtn").unbind("click").click(function() {
			router.route("commodityPriceCollection/commodityPriceCollection.html");
		});

		$("#main").on("click", ".collectionDelete", function() {
			var $this = $(this);
			dialog.showConfirmDialog("商品价格采集删除", "是否确认删除该条数据？", function(){
				deletePriceCollection($this.parents("tr").attr("collectionId"));
			});
		});
	};

	//改变涨跌颜色
	var riseAndFallColor = function() {
		var riseAndFall = $(".riseAndFall");
		for (var i = 0, j = riseAndFall.length; i < j; i++) {
			if (riseAndFall[i].innerHTML.charAt(0) == "-") {
				$(riseAndFall[i]).css("color", "#33A55E");
			} else if (riseAndFall[i].innerHTML.charAt(0) == "+") {
				$(riseAndFall[i]).css("color", "#F1592A");
			} else {
			}
		}
	};

	//查询价格采集列表
	var inquirePriceCollectionList = function() {
		var region = $("#collectionRegion").val();
		var manufacturer = $("#collectionManufacturer").val();
		var specification = $("#collectionSpecification").val();
		var bulkstockType = $("#collectionType").val();
		var endDate = $("#collectionTimeEdn").val();
		var startDate = $("#collectionTimeStart").val();
		var bulkstockName = $("#collectionName").val();
		base.fnAjax({
			action: "inquireBulkstockCollectionPageList.json",
			async: false, //同步异步
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize,
				region: region,
				specification: specification,
				bulkstockType: bulkstockType,
				//				endDate:endDate,
				startDate: startDate,
				manufacturer: manufacturer,
				bulkstockName: bulkstockName
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#collectionListTpl").html();
					var html = juicer(tpl, data);
					$("#collectionListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl(
						$("#collectionPage"),
						totalRecordCount,
						data.pageIndex,
						data.pageSize,
						function(p) {
							pageIndex = p;
							inquirePriceCollectionList();
						});
					riseAndFallColor();

					$(".collectionDetail").unbind("click").click(function() {
						var bulkstockName = $(this).parent().parent().attr("bulkstockName");
						var specification = $(this).parent().parent().attr("specification");
						var collectionId = $(this).parent().parent().attr("collectionId");
						var region = $(this).parent().parent().attr("region");
						router.route("commodityPriceCollection/commodityPriceCollectionMonitor.html?bulkstockName=" + encodeURI(encodeURI(bulkstockName)) + "&specification=" + encodeURI(encodeURI(specification)) + "&region=" + encodeURI(encodeURI(region)) + "&collectionId=" + collectionId);
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	//删除价格采集
	var deletePriceCollection = function(collectionId) {
		base.fnAjax({
			action: "disableBulkstockCollection.json",
			async: false, //同步异步
			nocode: false, //是否要登陆，true为不用登陆
			oData: {
				collectionId: collectionId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					router.route("commodityPriceCollection/commodityPriceCollectionList.html");
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	return {
		init: init
	};
});